package com.ruoyi.cmdb.service.impl.rabbitmq.direct;


import com.ruoyi.cmdb.service.MessageService;
import org.springframework.amqp.core.AmqpTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;

@Service//有了service,springboot才能自动捕获到这个类
public class MessageServiceRabbitmqImpl implements MessageService {
    @Autowired
    private AmqpTemplate amqpTemplate;
    private ArrayList<String> msgList = new ArrayList<String>();

    @Override
    public void sendMessage(String exchange,String connection,String id) { //把id放入消息队列
        System.out.println("把id放入到队列:"+exchange+","+connection+","+id);
        amqpTemplate.convertAndSend(exchange,connection,id);//参数解释:此次连接的交换机是什么;交换机上有多个连接时,使用哪个连接,就是使用哪个对应的队列;要插入到队列的消息是啥
    }

    @Override
    public String doMessage(String queue) { //从队列中获取id,发送短信
        String id = (String) amqpTemplate.receiveAndConvert(queue);
        return id;
    }
}
